#include "weather_line_label.h"

weather_line_label::weather_line_label(QWidget *parent) : QLabel(parent)
{

}

void weather_line_label::paintEvent(QPaintEvent *event)
{
    QPainter painter_2(this); // 用于画温度折线

    QPen pen_2;
    QColor cur_color; // 默认为黄色
    cur_color = QColor(254, 188, 50);

    pen_2.setWidth(2);
    pen_2.setColor(cur_color);
    pen_2.setStyle(Qt::SolidLine);

    QBrush brush_2;
    brush_2.setColor(cur_color);
    brush_2.setStyle(Qt::SolidPattern);

    painter_2.setPen(pen_2);
    painter_2.setBrush(brush_2);
    painter_2.setRenderHint(QPainter::Antialiasing, true); // 开启 QPainter 的抗锯齿，让曲线边缘更光滑

    QPoint tem_data[7]; // 用于存储七个数据的坐标
    int max_tem = 0; // 数据当中的最高温度(当前可以是一天中的最高温度，也可以是最低温度)
    int min_tem = this->hotest_data[0]; // 数据当中的最低温度
    int tem_unit = 0; // 单位温度(最高-最低 / 7)

    for (int i = 0; i < 7; i++)
    {
        max_tem = max_tem > this->hotest_data[i] ? max_tem : hotest_data[i];
        min_tem = min_tem < this->hotest_data[i] ? min_tem : hotest_data[i];
    }
    tem_unit = (max_tem - min_tem) / 7;

    for (int i = 0; i < 7; i++)
    {
        tem_data[i].setX((i * 90) + 60);
        int cur_Y = (qAbs(max_tem - hotest_data[i]) * 2) + 30;
        tem_data[i].setY(cur_Y);
    }
    painter_2.drawPolyline(tem_data,7);

    for (int i = 0; i < 7; i++)
    {
        painter_2.drawEllipse(tem_data[i].x(), tem_data[i].y() - 3, 5,5); // 画原点
        QString tem_text = QString::number(this->hotest_data[i]) + QString("℃"); // 显示温度
        painter_2.drawText(tem_data[i].x(), tem_data[i].y() - 10, tem_text); // 画温度
    }


    cur_color = QColor(77, 175, 210);
    pen_2.setColor(cur_color);
    pen_2.setStyle(Qt::SolidLine);

    brush_2.setColor(cur_color);
    brush_2.setStyle(Qt::SolidPattern);

    painter_2.setPen(pen_2);
    painter_2.setBrush(brush_2);

    max_tem = 0; // 数据当中的最高温度(当前可以是一天中的最高温度，也可以是最低温度)
    min_tem = this->coldest_data[0]; // 数据当中的最低温度
    tem_unit = 0; // 单位温度(最高-最低 / 7)

    for (int i = 0; i < 7; i++)
    {
        max_tem = max_tem > this->coldest_data[i] ? max_tem : coldest_data[i];
        min_tem = min_tem < this->coldest_data[i] ? min_tem : coldest_data[i];
    }
    tem_unit = (max_tem - min_tem) / 7;

    for (int i = 0; i < 7; i++)
    {
        tem_data[i].setX((i * 90) + 60);
        int cur_Y = (qAbs(max_tem - coldest_data[i]) * 2) + 75;
        tem_data[i].setY(cur_Y);
    }

    painter_2.drawPolyline(tem_data,7);

    for (int i = 0; i < 7; i++)
    {
        painter_2.drawEllipse(tem_data[i].x(), tem_data[i].y() - 3, 5,5); // 画原点
        QString tem_text = QString::number(this->coldest_data[i]) + QString("℃"); // 显示温度
        painter_2.drawText(tem_data[i].x(), tem_data[i].y() - 10, tem_text); // 画温度
    }
}
